// pagesEnterprise/pages/newsList/newsList.js
const app = getApp()
import ajax from "../../../utils/index"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tagList: [ // '海报', '资讯', '朋友圈', '园区活动'
      {
        name: '海报',
        content: '多种营销类海报定制使用。'
      },
      {
        name: '资讯',
        content: '热点资讯，福利政策，一键分享。'
      },
      {
        name: '朋友圈',
        content: '转发朋友圈，精准触达。'
      },
      {
        name: '园区活动',
        content: '园区活动，在线参与。'
      }
    ],
    tagIndex: 0,
    tagChildrenList: ['全部'],
    wechattagChildrenList: ['全部', '获客赚佣', '推荐注册'],
    tagChildrenIndex: 0,
    wechatZoneExplainShow: true,
    pageSize: 1,
    readchBottomShow: false,
    modeIndex: '1'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const userInfo = wx.getStorageSync('userInfo')
    this.setData({
      author_img: userInfo.headimgUrl
    })
    this.get_poster_classifyList()
    this.setData({
      tagIndex: parseInt(options.tagIndex || '0'),
      modeIndex: wx.getStorageSync('marketing-guidance') ? null : '1'
    })
    options.tagIndex ? this.tagChange() : null
  },

  modeEvent() {
    let modeIndex = this.data.modeIndex;
    modeIndex = Number.isInteger(parseInt(modeIndex)) ? parseInt(modeIndex) + 1 : '0'
    if (modeIndex > 5) {
      modeIndex = null;
      wx.setStorageSync('marketing-guidance', true)
    }
    this.setData({ modeIndex })
  },
  /**
   * reachbottom
   */
  onReachBottom: function () {
    if (this.data.readchBottomShow) {
      return
    }
    if (this.data.tagIndex == 0) { //banner
      let pageSize1 = this.data.pageSize1
      pageSize1++
      this.setData({
        pageSize1
      })
      this.get_material_list(this.data.material_list[this.data.tagChildrenIndex])
    } else if (this.data.tagIndex == 1) { //news

    } else if (this.data.tagIndex == 2) { //wechat

    } else if (this.data.tagIndex == 3) { //active

    }
  },

  /**
   * events - tag change
   */
  tagChange: function (e) {

    const tagIndex = e?.currentTarget?.id || this.data.tagIndex
    console.log("tagChange--------------", tagIndex, e)
    this.setData({
      tagIndex: tagIndex,
      tagChildrenIndex: 0,
      pageSize: 1,
    })
    if (tagIndex == 0) {
      console.log("this.data.material_list----", this.data.material_list)
      if (this.data.material_list && this.data.material_list.length > 0) this.get_material_list(this.data.material_list[0])
    } else if (tagIndex == 1) {
      this.get_artcle_list()
    } else if (tagIndex == 2) {
      this.get_wechat_zone_text_list()
      this.get_wechat_zone_text_history()
    }
  },

  /**
   * events - tagChildren Change
   */
  tagChildrenChange: function (e) {
    this.setData({
      tagChildrenIndex: e.currentTarget.id
    })
  },

  /**
   * events - tagChildren Change for banner
   */
  bannerTagChildrenChange: function (e) {
    this.setData({
      tagChildrenIndex: e.currentTarget.id
    })

    this.get_material_list(this.data.material_list[e.currentTarget.id])
  },

  /**
   * events -  tagChildren Change for news
   */
  newsTagChildrenChange: function (e) {
    this.setData({
      tagChildrenIndex: e.currentTarget.id
    })

  },

  /**
   * events - tagChildren Change for wechat
   */
  wechatTagChildrenChange: function (e) {
    this.setData({
      tagChildrenIndex: e.currentTarget.id
    })
    this.get_wechat_zone_text_list(e.currentTarget.id)
  },

  /**
   * route - News Detail
   */
  routeNewsDetail: function (e) {
    wx.navigateTo({
      url: '/pagesEnterprise/pages/newsInformationDetail/newsInformationDetail?id=' + e.currentTarget.dataset.item.id,
    })
  },

  /**
   * route - banner canvas detail
   */
  routeBannerCanvas: function (e) {
    let item = e.currentTarget.dataset.item
    // console.log(item)
    wx.navigateTo({
      url: '/pages/share/canvas/canvas?id=' + item.categoryId
    })
  },

  /**
   * route - intro img
   */
  routeIntro: function () {
    wx.navigateTo({
      url: './intro/intro',
    })
  },

  /**
   * route - active detail
   */
  routeActiveDetail: function (e) {
    wx.navigateTo({
      url: '/pages/home/marketing/active1/marketing',
    })
  },

  /**
   * events - explain show hidden
   */
  wechatZoneExplainShow: function () {
    if (this.data.wechatZoneExplainShow) {
      this.setData({
        wechatZoneExplainShow: false
      })
    } else {
      this.setData({
        wechatZoneExplainShow: true
      })
    }
  },

  /**
   * request - banner category list
   */
  async get_poster_classifyList() {
    const res = await ajax.get('/wx/appletPoster/classifyList', { mediaType: 5, type: 1 });
    this.setData({
      material_list: res.rows
    })
    this.get_material_list(res.rows[0])
  },

  /**
   * request - banner list
   */
  async get_material_list(item) {
    ajax.showLoading()
    const res = await ajax.get('/wx/appletPoster/posterList', {
      categoryId: item.id,
      mediaType: item.mediaType,
      pageNum: this.data.pageSize,
      pageSize: 30
    }).finally(() => wx.hideLoading())
    this.setData({ bannerList: res.rows })
  },

  /**
   * request - article list
   */
  async get_artcle_list() {
    ajax.showLoading()
    const res = await ajax.get('/wx/AppletArticle/list', {
      sign: 0,//0:小程序文章 1：园区文章
      pageNum: this.data.pageSize,
      pageSize: 10
    }).finally(() => wx.hideLoading())
    this.setData({ article_list: res.rows })
  },

  /**
   * request - wechat zone list
   */
  async get_wechat_zone_text_list(type = '') {
    ajax.showLoading()
    const res = await ajax.get('/wx/appletMoments/list', {
      type: type == 0 ? '' : type,
      pageNum: this.data.pageSize,
      pageSize: 10
    }).finally(() => wx.hideLoading())
    this.setData({ wechat_zone_text_list: res.rows })
  },

  /**
   * request - wechat zone share records
   */
  async get_wechat_zone_text_history() {
    ajax.showLoading()
    const res = await ajax.get('/wx/appletLog/list', {
      pageNum: 1,
      pageSize: 10
    }).finally(() => wx.hideLoading())
    let rows = res.rows.map(ele => {
      ele.userName = ele.userName.substr(0, 1)
      return ele
    })
    this.setData({
      wechat_zone_text_records_list: rows
    })
  },

  /**
   * events - wechat zone copy text
   */
  copyWechatZone: function (e) {
    wx.setClipboardData({
      data: e.currentTarget.dataset.item.momentContent,
      success(res) {

      }
    })
    const userInfo = wx.getStorageSync('userInfo')
    ajax.get('/wx/appletMoments/clickCopy', {
      id: e.currentTarget.dataset.item.id,
      userId: userInfo.id
    })
  },

  /**
   * events save image
   */
  saveImg: function (e) {
    wx.previewImage({
      urls: [e.currentTarget.dataset.url]
    })
  },

})